package com.cnfangmao.masterdata.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.FieldRuleDto;
import com.cnfangmao.masterdata.dto.FieldRuleSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.FieldRule;
import com.cnfangmao.masterdata.service.FieldRuleService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 字段规则关联表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@RequestMapping("/fieldRule")
@Api(tags="1-3、配置-字段规则关联管理")
@RestController
public class FieldRuleController {

	private static final Logger logger =LoggerFactory.getLogger(FieldRuleController.class);

	@Autowired
	private FieldRuleService fieldRuleService;
	
	@PostMapping(value="/list.do")
	@ApiOperation(value="字段规则关联信息列表")
	@ApiImplicitParam(name="dto",value="查询字段规则关联信息条件",required=true,paramType="body",dataType="GeneralSearchDto«FieldRuleSearchDto»")
	public ResBody<GeneralPageVo<FieldRule>> listFieldRule(@RequestBody GeneralSearchDto<FieldRuleSearchDto> dto) {
		ResBody<GeneralPageVo<FieldRule>> resBody =new ResBody<GeneralPageVo<FieldRule>>();
		try {
			IPage<FieldRule> fieldRule=fieldRuleService.queryListFieldRule(dto);
			GeneralPageVo<FieldRule> result= new GeneralPageVo<FieldRule>();
			result.setPageNo(fieldRule.getCurrent());
			result.setPageSize(fieldRule.getPages());
			result.setRecords(fieldRule.getRecords());
			result.setTotalCount(fieldRule.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in FieldRuleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;	
	}
	
	@PostMapping(value="/create.do")
	@ApiOperation(value="创建字段规则信息")
	@ApiImplicitParam(name="dto",value="创建字段规则信息",required=true,paramType="body",dataType="FieldRuleDto")
	public ResBody<Boolean> createFieldRule(@RequestBody FieldRuleDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getRuleId().isEmpty() || dto.getTargetId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("规则Id或者规则作用目标为空");
				return resBody;
			}
			if(fieldRuleService.findByRuleIdAndTargetId(dto.getRuleId(), dto.getTargetId())!=null)
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("字段规则已经存在");
				return resBody;
			}
			dto.setFieldRuleId(SnowFlakeUtils.generator());
			FieldRule fieldRule = ModelUtils.parseObject(dto, FieldRule.class, "create");
			fieldRuleService.save(fieldRule);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run create error in FieldRuleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;	
	}
	
	@PostMapping(value="/update.do")
	@ApiOperation(value="修改字段规则信息")
	@ApiImplicitParam(name="dto",value="修改字段规则信息",required=true,paramType="body",dataType="FieldRuleDto")
	public ResBody<Boolean> updateFieldRule(@RequestBody FieldRuleDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getRuleId().isEmpty() || dto.getTargetId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("规则Id或者规则作用目标为空");
				return resBody;
			}
			FieldRule fieldRule=fieldRuleService.findByRuleIdAndTargetId(dto.getRuleId(), dto.getTargetId());
			if(fieldRule!=null && !fieldRule.getFieldRuleId().equals(dto.getFieldRuleId()))
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("字段规则已经存在");
				return resBody;
			}
			FieldRule fieldRuleEntity = ModelUtils.parseObject(dto, FieldRule.class, "update");
			fieldRuleService.updateById(fieldRuleEntity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("修改成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run update error in FieldRuleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		
		return resBody;	
	}
	
	@PostMapping(value="/delete.do")
	@ApiOperation(value="删除字段规则信息")
	@ApiImplicitParam(name="fieldRuleId",value="删除字段规则信息 fieldRuleId",required=true,paramType="body",dataType="string")
	public ResBody<Boolean> deleteFieldRule(@RequestBody  String fieldRuleId) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			fieldRuleService.removeById(fieldRuleId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run delete error in FieldRuleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;		
	}
	
	@PostMapping(value="/getFieldRule.do")
	@ApiOperation(value="获取字段规则信息")
	@ApiImplicitParam(name="fieldRuleId",value="获取字段规则信息 fieldRuleId",required=true,paramType="body",dataType="string")
	public ResBody<FieldRule> getFieldRule(@RequestBody String fieldRuleId) {
		ResBody<FieldRule> resBody =new ResBody<FieldRule>();
		try {
			FieldRule fieldRule =fieldRuleService.getById(fieldRuleId);
          	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(fieldRule);
		} catch (Exception e) {
			logger.error(String.format("run getFieldRule error in FieldRuleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;	
	}
}

